import { createApp } from 'vue'
import './style.css'
import App from './App.vue'

// 创建Vue应用
createApp(App).mount('#app')

// 注册Service Worker for PWA
if ('serviceWorker' in navigator) {
  window.addEventListener('load', async () => {
    try {
      const registration = await navigator.serviceWorker.register('/sw.js', {
        scope: '/'
      })
      
      console.log('Service Worker registered successfully:', registration.scope)
      
      // 监听Service Worker更新
      registration.addEventListener('updatefound', () => {
        const newWorker = registration.installing
        if (newWorker) {
          newWorker.addEventListener('statechange', () => {
            if (newWorker.state === 'installed' && navigator.serviceWorker.controller) {
              // 新的Service Worker已安装，提示用户刷新
              console.log('New content is available; please refresh.')
              // 可以在这里添加用户提示逻辑
            }
          })
        }
      })
      
    } catch (error) {
      console.error('Service Worker registration failed:', error)
    }
  })
  
  // 监听Service Worker消息
  navigator.serviceWorker.addEventListener('message', (event) => {
    console.log('Message from Service Worker:', event.data)
  })
}